<?php

namespace thinkct\library;

use GuzzleHttp\Client;
use think\facade\Request;
use think\facade\Cache;

class Verify
{
    protected $request;
    protected $apiQuery;
    public function __construct()
    {
        $this->request = Request::instance();
        $this->apiQuery = base64_decode('aHR0cHM6Ly92Mi50aGlua2N0Lm5ldC9hcGkvdjIvcXVlcnk=');
    }

    public function query(): array
    {
        try {
            $result = Cache::get('sysQuery');

            if (!isset($result['today']) || $result['today'] !== $this->getToday()) {
                $client = new Client();
                $response = $client->post($this->apiQuery, [
                    'form_params' => [
                        'name' => 'openai',
                        'host' => $this->request->host(),
                        'version' => '1.0.0',
                    ],
                    'verify' => false,
                ]);

                $result = json_decode($response->getBody()->getContents(), true);
                $result['code'] !== 1 ? die : null;

                Cache::set('sysQuery', [
                    'today' => $this->getToday(),
                    'data' => $result['data']
                ]);

            }
            return $result['data'];
        } catch (\Exception $exception) {
            return [];
        }
    }

    private function getToday(): string
    {
        return date('Y-m-d', time());
    }
}